package com.aischool.eduorg.api;

import com.aischool.params.AiArticleParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiAppArticleService;
import com.aischool.service.AiArticleService;
import com.aischool.service.AiArticlecatService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.StateEnum;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;

@RouteMapping(url = "/api/eduorg/article")
public class ArticleController extends Controller {

	@Inject
	AiArticleService aiArticleService;
	@Inject
	AiArticlecatService aiArticlecatService;
	@Inject
	AiAppArticleService aiAppArticleService;
	
	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiArticleParams params = new AiArticleParams();
		
		// 获取参数
		int org_id = this.getAttrForInt("login_org_id");
		
		int initno = getInt("initno", 0);
		int type_id = getParaToInt("type_id",0);
		
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
		
		params.setOrgId(org_id);
		
		params.setState(getInt("state", StateEnum.ALL.getState()));
		params.setUseState(getParaToInt("usestate", -1));
		
		params.setKeywords(get("keywords",""));
		
		params.setCatId(getInt("cat_id",0));
		params.setTypeId(getInt("type_id",0));
		
		if (initno == 1 && type_id > 0) {
			Object obj = aiArticlecatService.getOrgCat(org_id, type_id, 1);
			if (obj != null) { jr.setDicKV("listcat", obj); }
		}
		// 获取分页
		Object pageData = aiArticleService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	/**
	 * 获取单个信息
	 */
	public void getOne() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 获取单个用户信息
		int org_id = this.getAttrForInt("login_org_id");
		
		int article_id = getParaToInt("id",0);
		int type_id = getParaToInt("type_id",0);
		
		
		Object obj = aiArticlecatService.getOrgCat(org_id, type_id, 1);
		if (obj != null) { jr.setDicKV("listcat", obj); }
		
		if (article_id > 0) {
			obj = aiArticleService.getOne(article_id);
			if (obj != null) { jr.setDicKV("oneinfo", obj); }
		}

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	

	public void usestate() {
		// 结果类
		JsonResult jr = new JsonResult();
		int app_id = this.getAttrForInt("login_app_id");
		int org_id = this.getAttrForInt("login_org_id");
		int use_state = getInt(0);
		String ids = getPara("ids", "");
		
		if (app_id <= 0 || org_id <= 0) {
			jr.setCode(400);
			jr.setMsg("缺少参数！");
		}else {
			boolean bf = aiAppArticleService.updateAppAtricle(app_id, org_id, ids, use_state);
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		
		
		renderJson(jr.Dic);
		
	}
}
